﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace OCRUI.OCRBatchManager
{
    public class BoundedTextBase<T> : IBoundedText where T : IBoundedText 
    {
        public List<T> ChildItems { get; set; }
        /// <summary>
        /// Calculates the extend of a List of Boxed items
        /// </summary>
        /// <param name="items">List of items having bounds (boxes)</param>
        /// <returns>Rectangle describing the min x,y and the total size of the contents of the items</returns>
        public Rectangle GetBoundExtent(List<T> items)
        {
            Rectangle rBound = new Rectangle(int.MaxValue, int.MaxValue, 0, 0);
            foreach (IBoundedText c in items)
            {
                rBound.X = Math.Min(c.Bounds.Left, rBound.Left);
                rBound.Y = Math.Min(c.Bounds.Top, rBound.Top);
                rBound.Width = c.Bounds.Width + rBound.Width;
                rBound.Height = c.Bounds.Height + rBound.Height;

            }
            return rBound;
        }

        /// <summary>
        /// Gets the text in the children of this item.
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public string ToString(List<T> items)
        {
            StringBuilder sb = new StringBuilder();
            foreach (T item in items)
            {
                sb.Append(item.ToString());
            }
            return sb.ToString();
        }

       

        /// <summary>
        /// Returns a rectangle that contains the children of this Bounded item.
        /// </summary>
        public Rectangle Bounds
        {
            get
            {
                return this.GetBoundExtent(ChildItems);
            }
        }
        

        #region IBoundedText Members


        public string Parse(List<string> text)
        {
            throw new NotImplementedException();
        }

        public int BoundTextStartPos
        {
        get;set;
            
        }

        public int BoundTextEndPos
        {
            get;set;
        }

        #endregion
    }
}
